package edu.rmit.server;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class ServerGUI extends JPanel{
	private static int HEIGHT = 200;
	
	private static int WIDTH = 400;
	
	private Server server;
	
	private JLabel serverAddressLabel;
	
	private JLabel serverPortLabel;
	
	private JTextField serverAddressField;
	
	private JTextField serverPortField;
	
	private JButton startServerButton;
	
	private JButton exitServerButton;
	
	private String hostName = "127.0.0.1";
	
	private String port = "1099";
	
	
	public ServerGUI() {
		server = new Server();
		
		setLayout(new GridLayout(3,1));
		
		serverAddressLabel = new JLabel("Host:");
		serverPortLabel = new JLabel("Port:");
		
		serverAddressField = new JTextField(hostName,10);
		serverPortField = new JTextField(port,4);
		
		startServerButton = new JButton("Start");
		exitServerButton = new JButton("Exit");
		
		startServerButton.addActionListener(new StartServerButtonListener());
		exitServerButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				int chooseOption = JOptionPane.showConfirmDialog(null, 
						"Are you sure to exit the server?", 
						"The server exit may cause the client error!", 
						JOptionPane.YES_NO_OPTION);
				if(chooseOption == JOptionPane.YES_OPTION){
					System.exit(0);
				}
			}
		});
		
		JPanel hostPanel = new JPanel();
		
		hostPanel.add(serverAddressLabel);
		hostPanel.add(serverAddressField);
        add(hostPanel);
        
        JPanel portPanel = new JPanel();
        
        portPanel.add(serverPortLabel);
        portPanel.add(serverPortField);
        add(portPanel);
        
        JPanel commandPanel = new JPanel();
        commandPanel.add(startServerButton);
        commandPanel.add(exitServerButton);
        
        add(commandPanel);
	}
	
	public static void main(String[] args) {
		JFrame frame = new JFrame("Server Window");
		frame.setContentPane(new ServerGUI());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setVisible(true);
		frame.setSize(WIDTH, HEIGHT);
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        frame.setLocation((screenSize.width - frameSize.width) / 2,
                          (screenSize.height - frameSize.height) / 2);
	}
	
	class StartServerButtonListener  implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent event) {
			String hostName = serverAddressField.getText();
			String portStr = serverPortField.getText();
			
			if(!hostName.trim().equals("")){
				server.setHostName(hostName);
			}
			if(!portStr.trim().equals("")){
				int port = 1099;
				try {
					port = Integer.parseInt(portStr);
				} catch (NumberFormatException e) {
					e.printStackTrace();
				}
				server.setPort(port);
			}
			server.startServer();
			startServerButton.setEnabled(false);
		}
	}
}
